package com.adk.backend.controller;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxOrderList;
import com.adk.backend.service.OrderService;
import com.adk.backend.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据导出控制器
 */
@Slf4j
@RestController
@RequestMapping("/export")
public class ExportController {
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private ExcelUtil excelUtil;
    
    /**
     * 导出订单列表
     */
    @GetMapping("/orders")
    public void exportOrders(
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Integer shipType,
            @RequestParam(required = false) Integer status,
            HttpServletResponse response) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("orderNo", orderNo);
            params.put("customerId", customerId);
            params.put("shipType", shipType);
            params.put("status", status);
            params.put("pageNum", 1);
            params.put("pageSize", 10000); // 导出所有数据
            
            PageResult<LxOrderList> pageResult = orderService.getOrderList(params);
            List<Map<String, Object>> data = new ArrayList<>();
            
            // 转换数据格式
            for (LxOrderList order : pageResult.getList()) {
                Map<String, Object> row = new HashMap<>();
                row.put("订单号", order.getOrderNo());
                row.put("客户简称", order.getCusShort());
                row.put("FBA单号", order.getFbaNo());
                row.put("运输方式", getShipTypeName(order.getShipType()));
                row.put("总箱数", order.getTotalCtns());
                row.put("计费重量", order.getShipWeigth());
                row.put("状态", getStatusName(order.getStatus()));
                row.put("创建时间", order.getAddTime());
                data.add(row);
            }
            
            String[] headers = {"订单号", "客户简称", "FBA单号", "运输方式", "总箱数", "计费重量", "状态", "创建时间"};
            excelUtil.exportExcel(response, "订单列表", headers, data);
        } catch (Exception e) {
            log.error("导出订单失败", e);
            try {
                response.getWriter().write("导出失败：" + e.getMessage());
            } catch (IOException ioException) {
                log.error("写入错误信息失败", ioException);
            }
        }
    }
    
    private String getShipTypeName(Integer type) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "沙特空运");
        map.put(2, "沙特海运拼柜");
        map.put(3, "阿联酋空运");
        map.put(4, "阿联酋海运");
        map.put(7, "沙特海运整柜");
        return map.getOrDefault(type, "未知");
    }
    
    private String getStatusName(Integer status) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "待处理");
        map.put(2, "处理中");
        map.put(3, "待收货");
        map.put(4, "已收货");
        return map.getOrDefault(status, "未知");
    }
}

